//
//  IslanMapController.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/5/12.
//  Copyright © 2016年 game. All rights reserved.
//

#import "IslanMapController.h"

#import "Diaochang_listModel.h"
#import "CustomPointAnnotation.h"
#import "CalloutMapAnnotation.h"
#import "CallOutAnnotationView.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
#import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入云检索功能所有的头文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件

@interface IslanMapController ()<BMKMapViewDelegate, BMKGeoCodeSearchDelegate>
{
    bool isGeoSearch;
    
    BMKGeoCodeSearch* _geocodesearch;
}
@property (nonatomic,strong) BMKMapView *mapView;
@end

@implementation IslanMapController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.app.myTabBar.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [_mapView viewWillAppear];
    
    _mapView.delegate = self;
    _geocodesearch.delegate = self;
}


- (IBAction)backClickAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    BMKMapView* mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    //    mapView.y = 64+selectViewhight-5;
    [self.view addSubview:mapView];
    //
    _mapView = mapView;
    [_mapView setMapType:BMKMapTypeStandard];
    
    _geocodesearch = [[BMKGeoCodeSearch alloc]init];
    [_mapView setZoomLevel:11];
    
    Diaochang_listModel *list = self.dataAry[0];
    
    isGeoSearch = false;
    CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0};
    
    pt = (CLLocationCoordinate2D){[list.latitude floatValue], [list.longitude floatValue]};
    
    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];
    reverseGeocodeSearchOption.reverseGeoPoint = pt;
    
    [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];

}
//根据anntation生成对应的View
- (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation
{
    NSString *AnnotationViewID = @"customAnnotation";
    
    if ([annotation isKindOfClass:[CustomPointAnnotation class]]) {
        
        BMKPinAnnotationView *annotationview = [[BMKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
        
        if (annotation.title.intValue==1) {
            annotationview.image = [UIImage imageNamed:@"free_point~iphone"];
        }else{
            annotationview.image = [UIImage imageNamed:@"charge_point~iphone"];
        }
        
        
        //可以通过title来判断 来给大头针不同的图片
        [annotationview setAnimatesDrop:NO];
        annotationview.canShowCallout = NO;//关闭自带的吹出框
        
        return annotationview;
    }
    
    return nil;
}

-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error
{
    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapView removeAnnotations:array];
    array = [NSArray arrayWithArray:_mapView.overlays];
    [_mapView removeOverlays:array];
    if (error == 0) {
        
        for (int i=0; i<self.dataAry.count; i++) {
            
            Diaochang_listModel *list = self.dataAry[i];
            
            CustomPointAnnotation* item = [[CustomPointAnnotation alloc]init];
            
            CLLocationCoordinate2D coor;
            coor.latitude = [list.latitude doubleValue];
            coor.longitude = [list.longitude doubleValue];
            
            item.coordinate = coor;
            item.title = list.charge_type;
            
            [_mapView addAnnotation:item];
        }
        
        Diaochang_listModel *list = self.dataAry.firstObject;
        CLLocationCoordinate2D coor;
        coor.latitude = [list.latitude doubleValue];
        coor.longitude = [list.longitude  doubleValue];
        //        _mapView.centerCoordinate = coor;
        
        [_mapView setCenterCoordinate:coor animated:YES];
    }
}

-(void)viewWillDisappear:(BOOL)animated
{
    [_mapView viewWillDisappear];

    _mapView.delegate = nil; // 不用时，置nil
    _geocodesearch.delegate = nil;

}

- (void)dealloc {
    
    _mapView = nil; // 不用时，置nil
    _geocodesearch = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
